﻿package com.designoutput.ui.controls
{
    import com.designoutput.event.NumberEvent;
    import com.designoutput.event.SelectEvent;
    import com.designoutput.icon.DropdownOver;
    
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

	/**
	 * A extend class from Dropdown, it has some features as DropDown, but also allow end user to type number.
	 * Allow add listener for event:<b>SelectEvent.COMMAND</b> to get a string val for the number typed in or selected.
	 * JUST BE USED FOR TEXT FONT SIZE AND LEADING NOW. Extend it as you wanted.
	 * 
	 */	
    public class WriteAbleDropdown extends Dropdown
    {
        internal var numInput:NumberInput;

        public function WriteAbleDropdown(toatlWidth:int, contentWidth:int)
        {
            super(toatlWidth, contentWidth);
			this.textField.visible = false;
			numInput = new NumberInput(toatlWidth - this.openButton.width - 1,0);
			numInput.addEventListener(NumberEvent.VALUE_CHANGE, this.numInputChange);
			addChild(numInput);
        }

		override protected function itemClick(evt:MouseEvent):void{
			if (this.item != null)
			{
				this.item.Select(false);
			}
			this.item = evt.currentTarget as SelectItem;
			this.item.Select();
			hidePopup();
			this.numInput.text = this.item.text;
			dispatchEvent(new SelectEvent(SelectEvent.COMMAND, this.item.name));
		}
		
		override public function Set(strVal:String) : void
		{
			if (this.item != null)
			{
				this.item.Select(false);
			}
			var included:Boolean = false;
			var index:int = 0;
			while (index < this.content.numChildren)
			{
				
				this.item = this.content.getChildAt(index) as SelectItem;
				if (this.item.name == strVal)
				{
					included = true;
					break;
				}
				index++;
			}
			if(included){
				this.numInput.text = this.item.text;
				this.item.Select();
			}else{
				this.numInput.text = strVal;
			}
		}
		
		private function numInputChange(evt:NumberEvent):void{
			var valStr:String =  evt.value.toString();
			var valNum:Number = Number(valStr);
			valStr = !isNaN(valNum) ? valStr : "0";
			this.Set(valStr);
			dispatchEvent(new SelectEvent(SelectEvent.COMMAND, valStr));
		}

    }
}
